package com.lzy.cloud.goods.facade;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.goods.dtos.*;
import com.lzy.cloud.goods.service.*;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by WIN7 on 2018/6/11.
 */
@Component
public class CarSaleFacade {

    @Autowired
    CarSaleService carSaleService;

    @Autowired
    CarImageService carImageService;

    @Autowired
    CarParamService carParamService;

    @Autowired
    CarConfigService carConfigService;

    @Autowired
    ConfigService configService;

    @Autowired
    ParamService paramService;

    @Autowired
    CarDraftService carDraftService;

    /**
     * 新增车型
     * @param carSaleDto
     * @return
     */
    public Boolean add(CarSaleDto carSaleDto){
        //新增车型
        Long id = carSaleService.add(carSaleDto);
        Ensure.that(id).isNotNull("00020001");
        //新增车型附属信息
        addCarMessage(id,carSaleDto);
        return true;
    }

    /**
     * 新增车型草稿
     * @param carSaleDto
     * @return
     */
    public Boolean addDraft(CarSaleDto carSaleDto){
        //新增车型
        Long id = carSaleService.add(carSaleDto);
        Ensure.that(id).isNotNull("00020001");
        //新增车型附属信息
        addCarMessage(id,carSaleDto);
        //新增车型草稿
        CarDraftDto carDraftDto = new CarDraftDto();
//        carDraftDto.setCarId(id);
        carDraftDto.setName(carSaleDto.getDraftName());
        carDraftService.add(carDraftDto);
        return true;
    }

    /**
     * 编辑车型
     * @param carSaleDto
     * @return
     */
    public Boolean update(CarSaleDto carSaleDto){
        /*更新房车展示图*/
        //删除之前的房车展示图
        CarImageDto ciDto = new CarImageDto();
        ciDto.setSourceId(carSaleDto.getId());
        ciDto.setSourceType(1);
        carImageService.delByKey(ciDto);
        /*房车参数关联数据*/
        //删除之前的房车参数关联数据
        CarParamDto carParamDto = new CarParamDto();
        carParamDto.setSourceId(carSaleDto.getId());
        carParamDto.setSourceType(1);
        carParamService.delByKey(carParamDto);
        /*删除车内配置详情*/
        //删除之前的车内配置详情数据
        CarConfigDto carConfigDto = new CarConfigDto();
        carConfigDto.setSourceId(carSaleDto.getId());
        carConfigDto.setSourceType(1);
        carConfigService.delByKey(carConfigDto);
        addCarMessage(carSaleDto.getId(),carSaleDto);
        return true;
    }

    /**
     * 根据id查询车型详情
     * @param id
     * @return
     */
    public CarSaleDto getDetail(Long id){
        CarSaleDto carSaleDto = carSaleService.selectById(id);
        Ensure.that(carSaleDto).isNotNull("");
        //查询房车展示图
        CarImageDto carImageDto = new CarImageDto();
        carImageDto.setSourceId(id);
        carImageDto.setSourceType(1);
        List<CarImageDto> carImageDtoList = carImageService.selectList(carImageDto);
        carSaleDto.setCarImageDtoList(carImageDtoList);
        //查询房车参数关联数据
        CarParamDto carParamDto = new CarParamDto();
        carParamDto.setSourceId(id);
        carParamDto.setSourceType(1);
        List<CarParamDto> carParamDtoList = carParamService.selectList(carParamDto);
        if(carParamDtoList != null && carParamDtoList.size() > 0){
            for (CarParamDto cpDto:carParamDtoList) {
                ParamDto paramDto = paramService.selectById(cpDto.getParamId());
                cpDto.setParamDto(paramDto);
            }
        }
        carSaleDto.setCarParamDtoList(carParamDtoList);
        //查询车内配置详情数据
        CarConfigDto carConfigDto = new CarConfigDto();
        carConfigDto.setSourceId(id);
        carConfigDto.setSourceType(1);
        List<CarConfigDto> carConfigDtoList = carConfigService.selectList(carConfigDto);
        if(carConfigDtoList != null && carConfigDtoList.size() > 0){
            for (CarConfigDto ccDto:carConfigDtoList) {
                ConfigDto configDto = configService.selectById(ccDto.getConfigId());
                ccDto.setConfigDto(configDto);
            }
        }
        carSaleDto.setCarConfigDtoList(carConfigDtoList);
        return carSaleDto;
    }


    //新增车型附属信息
    public Boolean addCarMessage(Long id,CarSaleDto carSaleDto){
        //新增房车展示图
        if(carSaleDto.getCarImageDtoList().size()>0) {
            for (CarImageDto carImageDto:carSaleDto.getCarImageDtoList()){
                carImageDto.setSourceType(1);
                carImageDto.setSourceId(id);
                carImageService.add(carImageDto);
            }
        }
        //新增房车参数关联数据
        if (carSaleDto.getCarParamDtoList().size()>0){
            //自定义数量最多100个
            ParamDto paramDto = new ParamDto();
            paramDto.setSourceId(id);
            paramDto.setSourceType(1);
            paramDto.setType(2);
            List<ParamDto> paramDtoList = paramService.selectList(paramDto);
            if(paramDtoList != null && paramDtoList.size() > 0) {
                Ensure.that(paramDtoList.size()).isNotGt(100, "00020002");
            }
            for (CarParamDto carParamDto:carSaleDto.getCarParamDtoList()) {
                carParamDto.setSourceType(1);
                carParamDto.setSourceId(id);
                carParamService.add(carParamDto);
            }
        }
        //新增车内配置详情
        if(carSaleDto.getCarConfigDtoList().size() > 0){
            //自定义数量最多100个
            ConfigDto cDto = new ConfigDto();
            cDto.setCarId(id);
            cDto.setSourceType(1);
            cDto.setMode(2);
            PageInfo<ConfigDto> pageInfo = configService.selectList(null,cDto);
            if(pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
                Ensure.that(pageInfo.getList().size()).isNotGt(100, "00020003");
            }
            for (CarConfigDto carConfigDto:carSaleDto.getCarConfigDtoList()) {
                carConfigDto.setSourceType(1);
                carConfigDto.setSourceId(id);
                carConfigService.add(carConfigDto);
            }
        }
        return true;
    }

}
